<template>
  <van-cell-group style="text-align: left">

    <van-field
        v-model="message.notifier"
        required
        clearable
        :disabled="!flag"
        label="通知人"
        placeholder="请输入通知人(自动获取当前人)"
    />
    <!--<van-cell required is-link arrow-direction="down">-->
    <!-- 使用 title 插槽来自定义标题 -->
    <template #title>
      <span aria-required="true">通知目标</span>
      <van-dropdown-menu>
        <van-dropdown-item change="change" v-model="this.message.receiver" :options="targetOption" :disabled="!flag" />
      </van-dropdown-menu>
    </template>
    <!--    </van-cell>-->

    <van-field
        v-model="message.noticeTypeNo"
        label="通知类型"
        placeholder="请输入通知类型"
        required
        :disabled="!flag"
    />

    <van-field
        v-model="message.noticeBody"
        label="通知内容"
        placeholder="请输入内容"
        required
        :disabled="!flag"
    />
    <div style="text-align: center">
      <van-button @click="this.submit()" v-if="flag" type="primary">发送通知</van-button>
      <van-button @click="this.receive()" v-if="!flag" type="primary">确定</van-button>
    </div>
  </van-cell-group>
</template>

<script>

import request from "../../util/request.js"

export default {
  name: "mSendNotice",
  data() {
    return {
      receiveBody:{openId:"",noticeNo:""},
      noticeNo:"",
      message:{
        receiver:"下拉选择通知目标",
      },
      value:"",
      targetOption:[],
      flag:false,
      userInformation : {}
    };
  },
  mounted() {
    this.getAllGrade()
    this.getRole()
    if (this.getUrlKey("noticeNo")){
      this.receiveBody.noticeNo=this.getUrlKey("noticeNo")
      this.receiveBody.openId=this.userInformation.openId
      this.getNoticeMain(this.receiveBody.noticeNo)
    }
  },
  created() {

  },
  methods:{
    // 获取url中的参数
    getUrlKey(name) {
      //获取url 参数
      return (
          decodeURIComponent(
              (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
                  location.href
              ) || [, ""])[1].replace(/\+/g, "%20")
          ) || null
      );
    },
    // 发送通知
    submit(){
      request.post("/wx_push_notice/push_message",this.message).then(res=>{
      })
    },
    // 确认收到通知
    receive(){
      request.post("/wx_push_notice/receive_message",this.receiveBody).then(res=>{

      })
    },
    //   获取通知目标下拉框内容
    getAllGrade(){
      request.get("/info_grade/allGrade").then(res=>{
        this.targetOption=res.data.data
      })
    },
    // 获取用户角色
    getRole(){
      this.userInformation = JSON.parse(localStorage.getItem("userInformation"));
      if (this.userInformation.roleNo==="01"||this.userInformation.roleNo==="03"){
        this.flag=true
      }
    },
    // 学生获取通知信息
    getNoticeMain(noticeNo){
      request.get("/notice_main/"+noticeNo).then(res=>{
        this.message=res.data.data
      })
    }
  },
};
</script>

<style scoped></style>
